package com.cryptape.cita_wallet.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.MapUtils;
import com.cryptape.cita_wallet.CytonApplication;
import com.cryptape.cita_wallet.adapter.MiningIndexAdapter;
import com.cryptape.cita_wallet.bean.LightNodeBean;
import com.cryptape.cita_wallet.bean.MarkIndex;
import com.cryptape.cita_wallet.bean.MarkIndexBean;
import com.cryptape.cita_wallet.bean.MarkIndexItem;
import com.cryptape.cita_wallet.net.callback.CommonCallback;
import com.cryptape.cita_wallet.net.request.HttpRequestMode;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.util.RSAUtils;
import com.cryptape.cita_wallet.util.db.DBWalletUtil;
import com.cryptape.cita_wallet.view.TitleBar;
import com.cryptape.cita_wallet.view.button.CommonButton;
import com.mangoworld.android.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Subscriber;

/**
 * Created by dengming on 9/3/20.
 */

public class MiningIndexActivity extends NBaseActivity {
    private RecyclerView recyclerView;
    private MiningIndexAdapter minerAdapter;
    private TitleBar mTitle;
    private TextView tv_balance_mgp;
    private TextView tv_balance_usd;
    private CommonButton btn_extract;
    private TextView tv_income_select_type;
    private TextView tv_income;
    private TextView tv_balance;
    private List<MarkIndexItem> datas = new ArrayList<>();


    @Override
    protected int getContentLayout() {
        return R.layout.activity_mining_index;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.miner_income_recycler);
        mTitle = findViewById(R.id.title_bar);
        btn_extract = findViewById(R.id.btn_extract);
        tv_income_select_type = findViewById(R.id.tv_income_select_type);
        tv_income = findViewById(R.id.tv_income);
    }

    @Override
    protected void initData() {
        initMinerData();
    }

    private int selectTypeIndex = 0;

    @Override
    protected void initAction() {
//        tv_balance.setText(getString(R.string.));
    }

//    private MiningOrderIncomeBean mData;
//    private MiningWithdrawDetailBean detailBean;

    private void initMinerData() {
        initAdapter();
        String address = DBWalletUtil.getCurrentWallet(CytonApplication.getInstance()).address;
        try {
            Map params = MapUtils.newHashMap();
            params.put("address", address);
            params.put("limit", "100");
            params.put("page", "1");
            String jsonData = GsonUtils.toJson(params);
            String content = RSAUtils.encrypt(jsonData);
            HttpRequestMode.getIndexMarkIndex(MiningIndexActivity.this, content, true, new CommonCallback() {
                @Override
                public void onSuccess(Object data, String successMsg, int responseSuccessCode) {
                    if (data == null) {
                        return;
                    }

                    MarkIndexBean markIndex = (MarkIndexBean) data;
                    if (markIndex == null) {
                        return;
                    }
                    tv_income.setText(markIndex.getNum());
                    List<MarkIndexItem> indexItems = markIndex.getList();
                    if (indexItems != null) {
                        datas.clear();
                        datas.addAll(indexItems);
                        minerAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onFailure(String msg, int responseFailCode) {
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
//        recyclerView.addItemDecoration(new SpacesItemDecoration(10));
//        MarkIndexItem item = new MarkIndexItem();
//        item.setCreateTime("2019");
//        item.setNum("300");
//        datas.add(item);
        minerAdapter = new MiningIndexAdapter(datas);
        recyclerView.setAdapter(minerAdapter);
    }

    private void extractIncome() {

    }
}
